package 牛客网_N.查找_递归_排序_贪心.寻找二维数组中的数;

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		int target = 2;
		int[][] A = new int[][]{{1,1}}; 
		System.out.println(searchMatrix(A, target));
	}

	/**
	 * 二维二分查找
	 * @param a
	 * @param target
	 * @return
	 */
	public static boolean searchMatrix(int[][] A, int target) {
		int max = A.length*A[0].length-1;
		int min = 0;
		while(min <= max){
			int mid = (max+min)/2;
			if(A[mid/A[0].length][mid%A[0].length] == target) return true;
			if(A[mid/A[0].length][mid%A[0].length] > target) max = mid-1;
			if(A[mid/A[0].length][mid%A[0].length] < target) min = mid+1;
		}
		return false;
	}
}
